import fs from 'fs'
import path from 'path'

export function getRandomImages(directoryPath, count) {
  try {
    const files = fs.readdirSync(directoryPath)
    const imageFiles = files.filter(file => 
      /\.(jpg|jpeg|png|gif)$/i.test(file)
    )
    
    const selectedImages = []
    const totalImages = imageFiles.length
    
    // 如果图片数量不足，则返回所有可用图片
    if (totalImages <= count) {
      return imageFiles.map(file => path.join(directoryPath, file))
    }
    
    // 随机选择指定数量的图片
    while (selectedImages.length < count) {
      const randomIndex = Math.floor(Math.random() * imageFiles.length)
      const image = imageFiles[randomIndex]
      
      if (!selectedImages.includes(image)) {
        selectedImages.push(path.join(directoryPath, image))
      }
    }
    
    return selectedImages
  } catch (error) {
    console.error('Error loading images:', error)
    return []
  }
} 